<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import * as echarts from 'echarts'
const unwarp = (obj) => obj && (obj.__v_raw || obj.valueOf() || obj);
export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '800px'
    },
    height: {
      type: String,
      default: '500px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    clearCharData(){
      this.chart.dispose();
      this.chart = echarts.init(this.$el, 'macarons');
      this.setOptions({'titleText':'','legend':[],'xAxisData':[],'seriesName':[],'seriesData':[]});
    },
    /**
     * 曲线调用数据处理方法
     * @param listData 曲线数据
     * @param xName   横坐标字段名
     * @param yName   纵坐标字段名
     * @param curveName 曲线名称
     * @param coverFlag  true 为新增曲线，覆盖，false 为增量曲线，常用作画对比曲线
     */
    setCharData(proxy,title,listData,xName,yName,curveName,coverFlag){
      let lineChartData=this.chart.getOption();
      lineChartData.titleText=title;
      let xAxisData = [];
      let seriesObj ={};
      let seriesData=[];
      let legend=[];
      if(coverFlag==false&&lineChartData.legend!=null&&lineChartData.legend.length>0){
        legend=lineChartData.legend[0].data;
      }else if(coverFlag=true){
        lineChartData.legend[0].data=[];
      }
      if(coverFlag==false&&lineChartData.xAxis!=null&&lineChartData.xAxis.length>0){
        xAxisData=lineChartData.xAxis[0].data;
      }else if(coverFlag=true){
        lineChartData.xAxis[0].data=[];
      }
      if(coverFlag==false&&lineChartData.series!=null&&lineChartData.series.length>0){
        let dataList=lineChartData.series;
        for(let i=0;i<dataList.length;i++){
          seriesObj[dataList[i].name]=dataList[i].data;
        }
      }else if(coverFlag=true){
        lineChartData.series=[];
      }
      legend.push(curveName);
      for (let i = 0; i < listData.length; i++) {
        let irCurveRate =  listData[i];
        xAxisData.push(irCurveRate[xName]);
        seriesData.push(proxy.formartNumber(irCurveRate[yName]*100, 4));
      }
      if (listData.length > 0) {
        lineChartData.titleText = title;
        lineChartData.seriesName=legend;
        seriesObj[curveName]=seriesData;
      }
      //去重
      xAxisData = proxy.lsh.uniq(xAxisData);
      //排序
      xAxisData.sort(function (a, b) {
        a = a.toUpperCase();
        b = b.toUpperCase();
        var ca = a.substr(a.length - 1, 1);
        var cb = b.substr(b.length - 1, 1);
        var va = a.substr(0, a.length - 1);
        var vb = b.substr(0, b.length - 1);
        if (ca == 'D') {
          va = proxy.lsh.multiply(parseFloat(va),1);
        } else if (ca == 'M') {
          va = proxy.lsh.multiply(parseFloat(va),30);
        } else if (ca == 'Y') {
          va = proxy.lsh.multiply(parseFloat(va),360);
        }
        if (cb == 'D') {
          vb =proxy.lsh.multiply(parseFloat(vb),1);
        } else if (cb == 'M') {
          vb = proxy.lsh.multiply(parseFloat(vb),30);
        } else if (cb == 'Y') {
          vb = proxy.lsh.multiply(parseFloat(vb),360);
        }
        return va - vb;
      });
      lineChartData.legend=legend;
      lineChartData.xAxisData = xAxisData;
      lineChartData.seriesData = seriesObj;
      if(coverFlag=true){
        lineChartData.yAxis=[]
        lineChartData.xAxis=[]
      }
      this.chartData.titleText=lineChartData.titleText;
      this.chartData.legend=lineChartData.legend;
      this.chartData.xAxisData=lineChartData.xAxisData;
      this.chartData.seriesName=lineChartData.seriesName;
      this.chartData.seriesData=lineChartData.seriesData;
      this.chart.dispose();
      this.initChart(this.chartData)
    },
    setOptions(expectedData) {
      unwarp(this.chart).setOption({
        title: {
          text: expectedData.titleText
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          }
        },
        legend: {
          data: expectedData.legend
        },
        grid: {
          left: '3%',
          right: '3%',
          bottom: '10%',
          top: '100px',
          containLabel: true
        },
        toolbox: {
          feature: {
            // saveAsImage: {}
          }
        },
        xAxis: {
          axisLine: {
            symbol: ['none', 'arrow']
          },
          name:'期限',
          nameLocation:'middle',
          nameGap:30,
          type: 'category',
          boundaryGap: false,
          data: expectedData.xAxisData
        },
        yAxis: {
          show:true,
          axisLine: {
            symbol: ['none', 'arrow']
          },
          name: '利率(%)',
          type: 'value'
        },
        series: this.setSeries(expectedData)
      },false);
    },
    setSeries(chartData){
      let series=[];
      if(chartData.seriesName!=null&&chartData.seriesName.length>0){
           for(let i = 0; i < chartData.seriesName.length; i++) {
             let item = {};
             item.name = chartData.seriesName[i];
             item.type = 'line';
             item.data = chartData.seriesData[item.name];
             item.smooth = true;
             series.push(item);
           }
           return series;
      }else{
        return [];
      }
    },
  }
}
</script>
